package wqh.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wqh.domain.ChannelData;
import wqh.pojo.MapRet;
import wqh.service.ChannelDataService;
import wqh.service.DeviceService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/water/history")
public class HistoryDataJSONController {

    private static org.slf4j.Logger logger = LoggerFactory.getLogger(HistoryDataJSONController.class);

    @Autowired
    private ChannelDataService channelDataService;

    @Autowired
    private DeviceService deviceService;

    @RequestMapping(value = "/getData",method = RequestMethod.GET)
    public Object getData(@RequestParam(value = "draw",required = true) int draw,
                          @RequestParam(value = "start",required = true) int start,
                          @RequestParam(value = "length",required = true) int length,
                          @RequestParam(value = "order[0][column]",required = true) int column,
                          @RequestParam(value = "order[0][dir]",required = true) String order,
                          @RequestParam(value = "search[value]",required = true) String search)
    {
        try {
            PageHelper.startPage(start / length + 1, length);
            List<ChannelData> channelDataList = new ArrayList<ChannelData>();
            if(column == 0 && search.equals("")) {
                if(order.equals("desc")){
                    channelDataList = channelDataService.findAllRecordDSEC_ID();
                }
                else{
                    channelDataList = channelDataService.findAllRecordASC_ID();
                }
            }
            else if(column == 1 && search.equals("")) {
                if(order.equals("desc")){
                    channelDataList = channelDataService.findAllRecordDSEC_TIME();
                }
                else{
                    channelDataList = channelDataService.findAllRecordASC_TIME();
                }
            }
            else if(column == 2 && search.equals("")) {
                if(order.equals("desc")){
                    channelDataList = channelDataService.findAllRecordDSEC_IP();
                }
                else{
                    channelDataList = channelDataService.findAllRecordASC_IP();
                }
            }
            else if(column == 0 && !search.equals("")) {
                if(order.equals("desc")){
                    channelDataList = channelDataService.findAllRecordDSEC_ID_IP_LIKE(search);
                }
                else{
                    channelDataList = channelDataService.findAllRecordASC_ID_IP_LIKE(search);
                }
            }
            else if(column == 1 && !search.equals("")) {
                if(order.equals("desc")){
                    channelDataList = channelDataService.findAllRecordDSEC_TIME_IP_LIKE(search);
                }
                else{
                    channelDataList = channelDataService.findAllRecordASC_TIME_IP_LIKE(search);
                }
            }
            else if(column == 2 && !search.equals("")) {
                if(order.equals("desc")){
                    channelDataList = channelDataService.findAllRecordDSEC_IP_LIKE(search);
                }
                else{
                    channelDataList = channelDataService.findAllRecordASC_IP_LIKE(search);
                }
            }
            else{
            channelDataList = channelDataService.findAllRecord();
            }
            Integer count = channelDataService.selectCount();
            @SuppressWarnings("unchecked")
            PageInfo page = new PageInfo(channelDataList);
            return MapRet.DATATABLES(draw,count,new Long(page.getTotal()).intValue(),channelDataList);

        }catch (Exception e)
        {
            e.printStackTrace();
            logger.error(e.toString());
            return MapRet.SQL_ERROR(e);
        }
    }

    @RequestMapping(value = "/select",method = RequestMethod.POST)
    public Object select(@RequestBody String json)
    {
        try {

            String deviceName = JSONObject.parseObject(json).getString("device_name");
            Byte channelNumber = JSONObject.parseObject(json).getByte("channel_number");
           if(!deviceName.equals("") && channelNumber != null){
                String ip = deviceService.findIpByName(deviceName);
                List<Map<Object,Object>> mapList = new ArrayList<Map<Object, Object>>();
                if(channelNumber == 1) mapList = channelDataService.findChannel_1_ByIp(ip);
                if(channelNumber == 2) mapList = channelDataService.findChannel_2_ByIp(ip);
                if(channelNumber == 3) mapList = channelDataService.findChannel_3_ByIp(ip);
                if(channelNumber == 4) mapList = channelDataService.findChannel_4_ByIp(ip);
                return MapRet.DATA(mapList);
           }
            return MapRet.DATA(null);
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.ERROR();
        }
    }

}
